*********************************************************************************
*                                                                               *
*                          Replication Garmann 2017     	        			*
*                                                                               *
*                                 Analysis 										*
*                                                                               *
*********************************************************************************


//Note: Dataset has already been -xtset- by Garmann
//timevariable is time; panel variable is gemeinde

*-----------------------------*
*	Load Data
*-----------------------------*

global beg_path `"C:\Users\JohnSmith\Dropbox\Replication_EF\"' 

cd `"${beg_path}Data\Analysis 2B\"'

   
 use Election_frequency.dta, clear

*-----------------------------*
*	Recoding
*-----------------------------*

encode artderwahl, gen(artderwahlb) // recode as categorical Variable

//Rename problematic variable with umlauts
rename *ber65 ueber65

replace datum = "27.1.2008" if datum == "27.01.2008" // for consistency (zeroes are not used for the other dates)  
encode datum, gen(election_fixed)

*-----------------------------*
*	Set Globals
*-----------------------------*

global controls 	"wahlberechtigte pop punter15 ueber65 sharefemale foreigners density bip_real parallel vscdu vsspd vsgreen vsfdp politicalcompetition number_voter_associations"
 


*-----------------------------*
*	Table 3 in Our Manuscript 
*-----------------------------*

keep if artderwahlb==1
eststo clear

*Replication of Column 1 with a different operationalisation (dummy, election in the last 100 days) & including the 2013 election
eststo M1: areg turnout days1_100 i.election_fixed if artderwahlb==1, cluster(gemeinde) absorb(gemeinde) 
estadd local municipality "Yes" , replace 
estadd local election  "Yes" , replace 
estadd local controls "No" , replace

*Replication of Column 2 with a different operationalisation (dummy, election in the last 100 days) & including the 2013 election
eststo M2: areg turnout days1_100 $controls  i.election_fixed if artderwahlb==1, cluster(gemeinde) absorb(gemeinde) 
estadd local municipality "Yes" , replace 
estadd local election  "Yes" , replace 
estadd local controls "Yes" , replace

merge 1:1 gemeindename jahr using index_n_w_5_to_merge.dta 
drop _merge

*Replication of Column 1 with our index 
cap gen jahr_since_1990 = jahr-1990

eststo M3: areg turnout index_n_w_5 c.jahr_since_1990 c.jahr_since_1990#c.jahr_since_1990, cluster(gemeinde) absorb(gemeinde ) //  
estadd local municipality "Yes" , replace 
estadd local election "No" , replace 
estadd local controls "No" , replace

*Replication of Column 2 with our index 
eststo M4: areg turnout index_n_w_5 c.jahr_since_1990 c.jahr_since_1990#c.jahr_since_1990 $controls , cluster(gemeinde) absorb(gemeinde) 
estadd local municipality "Yes" , replace 
estadd local election "No" , replace 
estadd local controls "Yes" , replace

cd `"${beg_path}Results"'

esttab M1 M2 M3 M4 using Table_3.tex, one incelldelimite("\:") stats(municipality election controls N r2, labels("Municipality FE" "Election FE" "Controls"  "N" "R2") layout("\multicolumn{1}{c}{@}" ///
"\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") fmt(0 0 2))  ///
		replace b(2) se(2) wide staraux ///
		star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none) order(days1_100 index_n_w_5 jahr_since_1990 c.jahr_since_1990#c.jahr_since_1990) keep(days1_100 index_n_w_5 jahr_since_1990 c.jahr_since_1990#c.jahr_since_1990 _cons) ///
		varlabels(days1_100 "Dummy: Days 1-100" index_n_w_5 "IEF" jahr_since_1990 "Years (since 1990)" c.jahr_since_1990#c.jahr_since_1990 "Years (since 1990)^2" _cons "Constant") ///
		nolegend note("") nomtitles alignment(d{10})

***********************
* Electronic Appendix *
***********************

*-----------------------------*
*	Table A 11
*-----------------------------*

*Replication of Garmann: Table 4 & Column 1 
eststo clear
eststo M1: areg turnout days i.election_fixed if  jahr <2013, cluster(gemeinde) absorb(gemeinde) //  Our result: .0027693; p = 0.001 
estadd local municipality "Yes" , replace 
estadd local election  "Yes" , replace 
estadd local controls "No" , replace 

*Replication of Garmann: Table 4 & Column 2 
eststo M2:  areg turnout days $controls  i.election_fixed if  jahr <2013, cluster(gemeinde) absorb(gemeinde) //  Our result: .0027693; p = 0.001 
estadd local municipality "Yes" , replace 
estadd local election  "Yes" , replace 
estadd local controls "Yes" , replace

*Replication of Column 1 with a different operationalisation (dummy, election in the last 100 days)
eststo M3: areg turnout days1_100 i.election_fixed if jahr <2013, cluster(gemeinde) absorb(gemeinde) //  Our result: .0027693; p = 0.001 
estadd local municipality "Yes" , replace 
estadd local election  "Yes" , replace 
estadd local controls "No" , replace

*Replication of Column 2 with a different operationalisation (dummy, election in the last 100 days) 
eststo M4: areg turnout days1_100  $controls  i.election_fixed if  jahr <2013, cluster(gemeinde) absorb(gemeinde) 
estadd local municipality "Yes" , replace 
estadd local election  "Yes" , replace 
estadd local controls "Yes" , replace  

*Replication of Column 1 with a different operationalisation (dummy, election in the last 100 days) & including the 2013 election
eststo M5: areg turnout days1_100 i.election_fixed if artderwahlb==1, cluster(gemeinde) absorb(gemeinde) 
estadd local municipality "Yes" , replace 
estadd local election  "Yes" , replace 
estadd local controls "No" , replace

*Replication of Column 2 with a different operationalisation (dummy, election in the last 100 days) & including the 2013 election
eststo M6: areg turnout days1_100 $controls  i.election_fixed if artderwahlb==1, cluster(gemeinde) absorb(gemeinde) 
estadd local municipality "Yes" , replace 
estadd local election  "Yes" , replace 
estadd local controls "Yes" , replace

*Replication of Column 1 with our index 
eststo M7: areg turnout index_n_w_5 c.jahr_since_1990 c.jahr_since_1990#c.jahr_since_1990, cluster(gemeinde) absorb(gemeinde ) //  
estadd local municipality "Yes" , replace 
estadd local election "No" , replace 
estadd local controls "No" , replace

*Replication of Column 2 with our index 
eststo M8: areg turnout index_n_w_5 c.jahr_since_1990 c.jahr_since_1990#c.jahr_since_1990 $controls , cluster(gemeinde) absorb(gemeinde) 
estadd local municipality "Yes" , replace 
estadd local election "No" , replace 
estadd local controls "Yes" , replace


esttab M1 M2 M3 M4 M5 M6 M7 M8 using A_Table_11.tex, one incelldelimite("\:") stats(municipality election N r2, labels("Municipality FE" "Election FE" "N" "R2") layout(  "\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{$@$}" ///
"\multicolumn{1}{c}{$@$}") fmt(0 0 2))  ///
		replace b(2) se(2) wide staraux ///
		star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none) order(days days1_100 index_n_w_5 jahr_since_1990 c.jahr_since_1990#c.jahr_since_1990)  drop(*.election*) ///
		varlabels(days "Days" days1_100 "Dummy: Days 1-100" index_n_w_5 "IEF" jahr_since_1990 "Years (since 1990)" c.jahr_since_1990#c.jahr_since_1990 "Years (since 1990)^2" _cons "Constant" days "Days" days1_100 "Dummy: Days 1 \textendash 100" /// 
		time_function "Days (IEF  function)" index_n_w_5 "IEF" jahr_since_1990 "Years (since 1990)" wahlberechtigte "Registered voters" ///
		pop "Population size" punter15 "Proportion of youth (0-15)"  ueber65 "Proportion of elderly (+65)" /// 
		sharefemale "Proportion of females" foreigners "Proportion of foreigners" density "Population density" ///
		bip_real "Real GDP per capita" parallel "Concurrent elections" vscdu "Vote share CDU" vsspd "Vote share SPD" ///
		vsgreen "Vote share Green" vsfdp "Vote share FDP" politicalcompetition "Political competition" /// 
		number_voter_associations "Number of local party lists") ///
		refcat(days "\textit{Election Frequency:}" jahr_since_1990 "\textit{Controls}" , nolabel) ///
		nolegend note("") nomtitles alignment(d{10})		
		

*-----------------------------*
*	Table A 12
*-----------------------------*		
		
*Robustness checks		

gen time_function = 1/exp(days/365.25)

eststo clear 

*Replication of Column 1 with a different operationalisation (dummy, election in the last 100 days)  
eststo M1: areg turnout time_function i.election_fixed if artderwahlb==1, cluster(gemeinde) absorb(gemeinde) 
estadd local municipality "Yes" , replace 
estadd local election  "Yes" , replace 
estadd local controls "No" , replace
estadd local lmuniptrend "No" , replace
estadd local qmuniptrend "No" , replace
*Replication of Column 2 with a different operationalisation (dummy, election in the last 100 days) 
eststo M2: areg turnout time_function $controls  i.election_fixed if artderwahlb==1, cluster(gemeinde) absorb(gemeinde)  
estadd local municipality "Yes" , replace 
estadd local election  "Yes" , replace 
estadd local controls "Yes" , replace
estadd local lmuniptrend "No" , replace
estadd local qmuniptrend "No" , replace
*IEF with municipality-specific linear trend 
eststo M3: areg turnout index_n_w_5 c.jahr_since_1990 c.jahr_since_1990#i.gemeinde $controls, cluster(gemeinde) absorb(gemeinde )  
estadd local municipality "Yes" , replace 
estadd local election "No" , replace 
estadd local controls "Yes" , replace
estadd local lmuniptrend "Yes" , replace
estadd local qmuniptrend "No" , replace
*IEF with municipality-specific quadratic trend 
eststo M4: areg turnout index_n_w_5 c.jahr_since_1990 c.jahr_since_1990#c.jahr_since_1990 c.jahr_since_1990#i.gemeinde c.jahr_since_1990#c.jahr_since_1990#i.gemeinde $controls , cluster(gemeinde) absorb(gemeinde) 
estadd local municipality "Yes" , replace 
estadd local election "No" , replace 
estadd local controls "Yes" , replace
estadd local lmuniptrend "Yes" , replace
estadd local qmuniptrend "Yes" , replace
 
esttab M1 M2 M3 M4  using A_Table_12.tex, one incelldelimite("\:")  stats(municipality election controls lmuniptrend qmuniptrend ///
 N r2, labels("Municipality FE" "Election FE" "Controls" "Municipality-specific linear trend" "Municipality-specific quadratic trend" "N" "R2") fmt(0 0 2) layout("\multicolumn{1}{c}{@}" ///
"\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}")) ///
		replace b(2) se(2) wide staraux ///
		star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none)  keep(time_function index_n_w_5  jahr_since_1990 c.jahr_since_1990#c.jahr_since_1990 _cons) order(time_function index_n_w_5 jahr c.jahr#c.jahr) ///
		varlabels(time_function "Days (IEF  function)" index_n_w_5 "IEF"  jahr_since_1990 "Years (since 1990)" c.jahr_since_1990#c.jahr_since_1990 "Years (since 1990)^2" _cons "Constant") ///
		nolegend note("") mtitles("\shortstack{\\Time function\\no controls}" "\shortstack{\\Time function\\controls}" "\shortstack{\\IEF\\linear trend}" "\shortstack{\\IEF\\quadratic trend}") alignment(d{10})
	
*-----------------------------*
*	Table A 10
*-----------------------------*

*Descriptive statistics (Table)
lab var days "Days"
lab var days1_100 "Dummy: Days 1 \textendash 100"
lab var time_function "Days (IEF  function)"
lab var index_n_w_5 "IEF"
lab var jahr_since_1990 "Years (since 1990)" 
lab var wahlberechtigte "Registered voters"
lab var pop "Population size"
lab var punter15 "Proportion of youth (0-15)" 
lab var ueber65 "Proportion of elderly (+65)"
lab var sharefemale "Proportion of females" 
lab var foreigners "Proportion of foreigners" 
lab var density "Population density"
lab var bip_real "Real GDP per capita"
lab var parallel "Concurrent elections"
lab var vscdu "Vote share CDU"
lab var vsspd "Vote share SPD"
lab var vsgreen "Vote share Green"
lab var vsfdp "Vote share FDP"
lab var politicalcompetition "Political competition" 
lab var number_voter_associations "Number of local party lists"

estpost tabstat days days1_100 time_function index_n_w_5 jahr_since_1990 $controls, stat(count mean sd min max ) c(s)   //  

esttab . using A_Table_10.tex, replace cells("count(fmt(%9.0f)) mean(fmt(%9.2f)) sd(fmt(%9.2f)) min(fmt(%9.2f)) max(fmt(%9.2f))" ) label varwidth(30) noobs ///
collabels("N" "Mean" "SD" "Min" "Max") nodep nonumb nomti modelwidth(6)
	
	